﻿using System.Windows;

using System.Windows.Controls;
using CCF20Model;
using System;
using System.Collections.Generic;
using WiseLinkWPF.Windows.Controls.GridView;
using WiseLinkWPF.Windows.Data;
using WiseLink.RDS.Resource;

namespace CCF20Module
{
    /// <summary>
    /// Interaction logic for ContactCustomer.xaml
    /// </summary>
    public partial class RepairEnd : Window
    {
        private Vehicle vehicle;
        
        private VehicleMaintenanceModel vmm;
        private VehicleModel vm;

        public RepairEnd(Vehicle vr, VehicleModel vehicleModel)
        {
            InitializeComponent();
            vehicle = vr;
            vm = vehicleModel;
            CCF20Entities ccf20Ent = new CCF20Entities();
            vmm = new VehicleMaintenanceModel(ccf20Ent);
            this.DataContext = vehicle;
        }

        private void Submit_Click(object sender, RoutedEventArgs e)
        {

            int temp;
            int serviceMileage;

            if (!Int32.TryParse(this.ServiceMileage.Text.Trim(), out serviceMileage))
            {
                Utility.ShowMessageBox(Message.RepairEnd_Wrong_Mileage);
                return;
            }

            if (Int32.TryParse(this.ServiceCost.Text.Trim(), out temp))
            {
                foreach (CombinedMaintenanceType mt in this.gridMaintenance.SelectedItems)
                {
                    vm.AddVehicleMaintenanceHistory(vehicle, mt,serviceMileage);
                }
                vm.VehicleRepairEnd(vehicle, temp, "客户实际出场");
                Utility.ShowMessageBox(Message.Save_Succeed);
                this.Close();
            }
            else
            {
                Utility.ShowMessageBox(Message.RepairEnd_Wrong_Cost);
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.gridMaintenance.ItemsSource = vmm.GetCombinedMaintenanceTypeByVehicle(vehicle);
            this.ServieDate.SelectedDate = DateTime.Now;
            this.ServiceMileage.Text = vehicle.CurrentMileage.ToString();

            
        }

        private void gridMaintenance_SelectionChanged(object sender, WiseLinkWPF.Windows.Controls.SelectionChangeEventArgs e)
        {
            //if (e.AddedItems.Count > 0)
            //{
            //    foreach (var item in e.AddedItems)
            //    {
            //        CombinedMaintenanceType selectedItem = item as CombinedMaintenanceType;

            //    }

            //}
        }
    }
}
